package com.iotechn.unimall.biz.service.orderlog;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.data.domain.OrderLogDO;
import com.iotechn.unimall.data.mapper.OrderLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 订单流水处理
 *
 * @author kyms
 * @date 2020-06-29 11:04
 */
@Service
public class OrderLogBizService {
    private static final Logger logger = LoggerFactory.getLogger(OrderLogBizService.class);
    @Autowired
    private OrderLogMapper orderLogMapper;

    //添加订单流水
    public void addOrderLog(OrderLogDO orderLogDO) {
        //如果order_no 上次和本次状态一致则不更新
        if (orderLogDO != null && !StringUtils.isEmpty(orderLogDO.getOrderNo()) && orderLogDO.getCurrentStatus() != null) {
            Date now = new Date();
            orderLogDO.setGmtUpdate(now);
            boolean isExist = orderLogMapper.selectCount(new EntityWrapper<OrderLogDO>()
                    .eq("order_no", orderLogDO.getOrderNo())
                    .eq("last_status", orderLogDO.getLastStatus())
                    .eq("current_status", orderLogDO.getLastStatus())) > 0;
            if (!isExist) {
                orderLogDO.setGmtCreate(now);
                orderLogMapper.insert(orderLogDO);
                logger.info("订单:{},上次状态:{},当前状态:{}",orderLogDO.getOrderNo(),orderLogDO.getLastStatus(),orderLogDO.getCurrentStatus());
            }
        }

    }
}
